import java.util.*;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-07-19
 * Time:20:55
 */
public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //这个二叉树的根节点
    //public TreeNode root;
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        //TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        //E.right = H;
        return A;
    }

    public TreeNode createTree2() {
        TreeNode A = new TreeNode('1');
        TreeNode B = new TreeNode('2');
        TreeNode C = new TreeNode('3');
        TreeNode D = new TreeNode('4');

        A.left = B;
        A.right = C;
        B.left = D;
        return A;
    }

    // 前序遍历
    void preOrder(TreeNode root) {

    }

    // 中序遍历
    void inOrder(TreeNode root) {

    }

    // 后序遍历
    void postOrde(TreeNode root) {

    }

    // 子问题思路  获取树中节点的个数
    int size(TreeNode root) {
        if (root == null) return 0;
        return size(root.left) + size(root.right) + 1;
    }

    //遍历思路：只要遍历到了节点 就nodeSize ++
    public static int nodeSize;

    void size2(TreeNode root) {
        if (root == null) return;
        nodeSize++;
        size2(root.left);
        size2(root.right);
    }

    // 获取叶子节点的个数
    //子问题：
    int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    //遍历：
    public static int leafsize;

    int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            leafsize++;
        }
        getLeafNodeCount2(root.left);
        getLeafNodeCount2(root.right);
        return leafsize;
    }
        /*

    获取第K层节点的个数

     */

//    int getKLevelNodeCount(TreeNode root, int k) {
//        if(root == null) return 0;
//        int sum = 0;
//        for (int i = 1; i <= k; i++) {
//            if (i == k){
//                sum += judgeNodeCount(root.left);
//                sum +=judgeNodeCount(root.right);
//            }
//        }
//        return sum;
//    }
//    int judgeNodeCount(TreeNode noed){
//        int count = 0;
//        count = noed.left == null ? 0:1;
//        count += noed.right == null ? 0:1;
//        return count;
//    }

    int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1)
                + getKLevelNodeCount(root.right, k - 1);
    }


    /*

     获取二叉树的高度

     时间复杂度：O(N)

     */

    int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
//        if(root.left == null && root.right == null) {
//            return 1;
//        }
        return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
    }


    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val) {
        if (root == null) return null;
        if (root.val == val) {
            return root;
        }
        TreeNode ret1 = find(root.left, val);
        if (ret1 != null) {
            return ret1;
        }
        TreeNode ret2 = find(root.right, val);
        if (ret2 != null) {
            return ret2;
        }
        return null;
    }


    //层序遍历:一层一层遍历，从左到右

    void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        //要利用队列先进先出的特点
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);//先将根节点的值传入
        while (!queue.isEmpty()) {
            //如果queue不为空就继续走，即当queue为空时结束
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
        System.out.println();
    }

    //利用队列形成二维数组的形式来遍历,使每一行都能够清楚地表示出来
    public List<List<Character>> levelOrder2(TreeNode root) {
        List<List<Character>> lists = new ArrayList<>();
        if (root == null) {
            return lists;
        }
        Queue<TreeNode> cur = new LinkedList<>();
        cur.offer(root);
        while (!cur.isEmpty()) {
            int size = cur.size();//给定每行所拥有的节点的个数
            List<Character> row = new ArrayList<>();//创建行队列，相当于二维数组里的行
            while (size > 0) {
                //row.add(cur.poll());
                TreeNode ch = cur.poll();
                size--;
                row.add(ch.val);
                if (ch.left != null) {
                    //将每个节点的左孩子树添加到队列当中
                    cur.offer(ch.left);
                }
                if (ch.right != null) {
                    //将每个节点的右孩子树添加到队列当中
                    cur.offer(ch.right);
                }
            }
            lists.add(row);
        }
        return lists;
    }



    // 判断一棵树是不是完全二叉树

    boolean isCompleteTree(TreeNode root) {
        //还是利用队列来解题
        //当是一个完全二叉树的时候我们传入的节点是连续的，且到最后传入全为null，如果不是完全二叉树就是空，节点，空这种类型
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur != null) {
                //当cur不为空的时候持续传入
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }

        while (!queue.isEmpty()) {
            TreeNode cur = queue.peek();
            if(cur != null) {
                //如果是完全二叉树的话，到这一个while里面全是null
                //不是完全二叉树
                return false;
            }else {
                queue.poll();
            }
        }
        return true;
    }

}

